//
//  EmojiCollectionView.m
//  Emoji表情
//
//  Created by zxy on 2019/5/29.
//  Copyright © 2019 zxy. All rights reserved.
//

#import "EmojiCollectionView.h"
#import <Masonry.h>
#import "FlyHorizontalFlowLauyout.h"
#import "EmojiCollectionViewCell.h"
#import "EmojiModel.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
NSInteger MaxNum = 21;
static NSString *const emojiCellID = @"emojiCellID";
@interface EmojiCollectionView()<UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>{
    NSInteger _selBottomIdx;
}
@property(nonatomic,strong)NSMutableArray *emojiArr;
@property(nonatomic,strong)FlyHorizontalFlowLauyout *emojiLayout;
@property(nonatomic,strong)UIPageControl *emojiPageCtl;
@property(nonatomic,strong)UICollectionView *emojiCollectionView;
@end
@implementation EmojiCollectionView


-(FlyHorizontalFlowLauyout *)emojiLayout{
    if(!_emojiLayout){
        _emojiLayout = [[FlyHorizontalFlowLauyout alloc]init];
        _emojiLayout.minimumLineSpacing = 0;
        _emojiLayout.minimumInteritemSpacing = 0;
        _emojiLayout.sectionInset = UIEdgeInsetsMake(5, 0, 0, 0);
        _emojiLayout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width / 7, 40);
    }
    return _emojiLayout;
}


-(NSMutableArray *)emojiArr{
    if(!_emojiArr){
        _emojiArr = [NSMutableArray array];
    }
    return _emojiArr;
}


#pragma mark -加载表情
-(void)loadEmoji{
    NSString *emojiPath = [[NSBundle mainBundle] pathForResource:@"InnerStickersInfo.plist" ofType:nil];
    NSArray *array = [NSArray arrayWithContentsOfFile:emojiPath];
    for (NSDictionary *dic in array) {
        NSMutableArray *emojiArray = [NSMutableArray array];
        EmojiModel *model = [EmojiModel mj_objectWithKeyValues:dic];
        [emojiArray addObjectsFromArray:model.emoticons];
        //添加删除按钮
        EmojiContentModel *deleteM = [EmojiContentModel new];
        deleteM.isDeleteBtn = YES;
        NSInteger count = emojiArray.count / MaxNum;
        for (NSInteger i = 0; i < count; i++) {
            //在每一页的最后添加删除按钮 21、 42
            [emojiArray insertObject:deleteM atIndex:MaxNum - 1];
            MaxNum += MaxNum;
        }
        //在整个表情数组最后添加删除按钮
        if(emojiArray.count % MaxNum != 0){
            [emojiArray addObject:deleteM];
        }
        [self.emojiArr addObject:emojiArray];
    }
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _selBottomIdx = 0;
        [self loadEmoji];
        //加载顶部背景View
        UIView *topView = [[UIView alloc]init];
        topView.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0];
        [self addSubview:topView];
        [topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self);
            make.height.mas_equalTo(160);
        }];
        
        //表情collectionview
        UICollectionView *emojiCollectionV = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.emojiLayout];
        emojiCollectionV.delegate = self;
        emojiCollectionV.dataSource = self;
        emojiCollectionV.pagingEnabled = YES;
        emojiCollectionV.showsHorizontalScrollIndicator = NO;
        emojiCollectionV.backgroundColor = topView.backgroundColor;
        [topView addSubview:emojiCollectionV];
        [emojiCollectionV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(topView);
            make.height.mas_equalTo(140);
        }];
        self.emojiCollectionView = emojiCollectionV;
        //注册cell
        [emojiCollectionV registerClass:[EmojiCollectionViewCell class] forCellWithReuseIdentifier:emojiCellID];
        
        //索引
        UIPageControl *emojiPageCtl = [[UIPageControl alloc]init];
        emojiPageCtl.currentPageIndicatorTintColor = [UIColor colorWithRed:233/255.0 green:64/255.0 blue:24/255.0 alpha:1.0];
        emojiPageCtl.pageIndicatorTintColor = [UIColor lightGrayColor];
        [topView addSubview:emojiPageCtl];
        emojiPageCtl.numberOfPages = 2;
        [emojiPageCtl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(emojiCollectionV.mas_bottom);
            make.centerX.equalTo(self);
            make.height.mas_equalTo(@20);
        }];
        self.emojiPageCtl = emojiPageCtl;
        //创建底部view
        UIView *bottomView = [[UIView alloc]init];
        bottomView.backgroundColor = [UIColor whiteColor];
        [self addSubview:bottomView];
        [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.top.equalTo(topView.mas_bottom);
            make.height.mas_equalTo(@40);
        }];
        
        UIButton *dogeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        dogeBtn.tag = 0;
        [dogeBtn setImage:[UIImage imageNamed:@"doge"] forState:UIControlStateNormal];
        [dogeBtn addTarget:self action:@selector(changeEmojiClick:) forControlEvents:UIControlEventTouchUpInside];
        [bottomView addSubview:dogeBtn];
        [dogeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(bottomView);
            make.width.height.mas_equalTo(@40);
        }];
        UIButton *xiaohahaBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        xiaohahaBtn.tag = 1;
        [xiaohahaBtn setImage:[UIImage imageNamed:@"xiaohaha"] forState:UIControlStateNormal];
        [xiaohahaBtn addTarget:self action:@selector(changeEmojiClick:) forControlEvents:UIControlEventTouchUpInside];
        [bottomView addSubview:xiaohahaBtn];
        [xiaohahaBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(bottomView);
            make.left.equalTo(dogeBtn.mas_right);
            make.width.height.mas_equalTo(@40);
        }];
    }
    return self;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [self.emojiArr[_selBottomIdx] count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    EmojiCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:emojiCellID forIndexPath:indexPath];
    NSArray *emojis = self.emojiArr[_selBottomIdx];
    EmojiContentModel *emojiM = emojis[indexPath.item];
    
    cell.emojiModel = emojiM;
    return  cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    EmojiContentModel *emojiM = self.emojiArr[_selBottomIdx][indexPath.item];
    if(_delegate && [_delegate respondsToSelector:@selector(selEmojiStr:imageName:isClickDeleteButton:)]){
        //将表情图片文字，拼接[]，以便区分普通文字
        [_delegate selEmojiStr:[NSString stringWithFormat:@"[%@]",emojiM.desc] imageName:emojiM.image isClickDeleteButton:emojiM.isDeleteBtn];
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSInteger idx = scrollView.contentOffset.x / scrollView.bounds.size.width;
    self.emojiPageCtl.currentPage = idx;
}

#pragma mark -切换表情
-(void)changeEmojiClick:(UIButton *)sender{
    _selBottomIdx = sender.tag;
    NSArray *emojis = self.emojiArr[_selBottomIdx];
    //一页最多显示20个表情图片，最后一个是删除按钮
    if(emojis.count > 21){
        self.emojiPageCtl.hidden = NO;
    }else{
        self.emojiPageCtl.hidden = YES;
    }
    [self.emojiCollectionView reloadData];
}

@end
